#!/bin/bash

C="-c"

[[ "$1" == "$C" ]] && echo 'compressing' || echo 'not compressing'

echo 'Make all files readable'
find ./ -type f -print0 | xargs -0 chmod 644

echo 'Make all shell files executable'
find ./ -name "*.sh" -print0 | xargs -0 chmod u+x
find ./ -name "*.pl" -print0 | xargs -0 chmod u+x
chmod u+x ./steal/js

echo 'Make all directories executable'
find ./ -type d -print0 | xargs -0 chmod 711

if [[ "$1" == "$C" ]];
    then
    for x in `ls -d */*.html | awk -F"/" '{print $1}'`
    do 
	./steal/js steal/buildjs $x/*.html -to $x
    done
    for x in `ls -d */*.html`
    do
	sed 's/steal\.js/steal\.production\.js/g' $x > $x.production
    done
fi

rsync -avP --include=".htaccess" --exclude="development-bundle" --exclude="*~" --exclude="*.sh" --exclude=".*"  --delete-excluded ./. richpsharp_richpsharp@ssh.phx.nearlyfreespeech.net:/home/public/lock5
ssh richpsharp_richpsharp@ssh.phx.nearlyfreespeech.net rename -v --yes -s/\.production// /home/public/lock5/*/*.production
ssh richpsharp_richpsharp@ssh.phx.nearlyfreespeech.net /home/public/lock5/compress.pl

if [[ "$1" == "$C" ]];
    then
    rm -fv */*.production
    rm -fv */production.*
fi
